var year = document.getElementById("year");
var month = document.getElementById("month");
var day = document.getElementById("day");
var days; // 表示天数
function creatDate() {
    //生成1990年-2500年
    for (var i = 1990; i <= 2500; i++) {
        var option = document.createElement('option');
        option.setAttribute('value', i);
        option.innerHTML = i;
        year.appendChild(option);
    }
    //生成1月-12月
    for (var j = 1; j <= 12; j++) {
        var option = document.createElement('option');
        option.setAttribute('value', j);
        option.innerHTML = j;
        month.appendChild(option);
    }
    //生成1日—31日
    for (var k = 1; k <= 31; k++) {
        var option = document.createElement('option');
        option.setAttribute('value', k);
        option.innerHTML = k;
        day.appendChild(option);
    }
}
creatDate();
year.onclick = function () {
    month.options[1].selected = true;
    day.options[1].selected = true;
}
month.onclick = function () {
    day.options[1].selected = true;
    if (this.value == 2) {
        //如果是闰年
        if ((year.value % 4 == 0 && year.value % 100 == 0) || year.value % 400 == 0) {
            days = 29;
        }
        else { // 平年
            days = 28;
        }
    }
    else if (month.value == 4 || month.value == 6 || month.value == 9 || month.value == 11) {
        days = 30;
    }
    else {
        days = 31;
    }
    //增加或删除天数
    if (days === 28) {
        day.remove(31);
        day.remove(30);
        day.remove(29);
    }
    if (days === 29) {
        day.remove(31);
        day.remove(30);
        //如果第29天不存在，则添加第29天
        if (!day.options[29]) {
            day.add(new Option('29', '29'), undefined)
        }
    }
    if (days === 30) {
        day.remove(31);
        if (!day.options[29]) {
            day.add(new Option('29', '29'), undefined)
        }
        //如果第30天不存在，则添加第30天
        if (!day.options[30]) {
            day.add(new Option('30', '30'), undefined)
        }
    }
    if (days === 31) {
        if (!day.options[29]) {
            day.add(new Option('29', '29'), undefined)
        }
        if (!day.options[30]) {
            day.add(new Option('30', '30'), undefined)
        }
        if (!day.options[31]) {
            day.add(new Option('31', '31'), undefined)
        }
    }
}